<template>
	<view>
		<mc-framework navTitle="MES工控机" @changeLevel="changeLevel">

			<template #content>
				<view class="content" style="height: 100%;width: 100%;">
					<view class="contentBox">
						<view class="container">


							<uni-forms ref="baseForm" :modelValue="baseFormData">
								<view class="form-item-row">
									<uni-forms-item label="设备名称:" label-width="auto">
										<input v-model="baseFormData.EquipmentName" type="text" placeholder="请输入设备名称"
											disabled />
									</uni-forms-item>

									<uni-forms-item label="点检人:" label-width="auto">
										<input v-model="baseFormData.CheckupMan" type="text" placeholder="请输入点检人"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="点检日期:" label-width="auto">
										<input v-model="baseFormData.BillDate" type="text" placeholder="请输入点检日期"
											disabled>
										<text class="text" @click="Timeshow=true"></text>
										</input>
									</uni-forms-item>
									<uni-forms-item label="周期:" label-width="auto">
										<input v-model="baseFormData.PeriodTypeName" type="text" placeholder="请输入周期"
											disabled />
										<text class="text" @click="PeriodTypeShow=true"></text>
									</uni-forms-item>
								</view>
							</uni-forms>
						</view>
						<view class="table">
							<table>
								<tr>

									<th>序号</th>
									<th>所属分类</th>
									<th>点检内容</th>
									<th>点检方式</th>
									<th>点检标准</th>
									<th width='25%'>点检结果</th>

								</tr>
								<tr v-for="(item, index) in tableData">

									<td>{{index + 1}}</td>
									<td>{{item.EquipmentCheckTypeName}}</td>
									<td>{{item.CheckupItemName}}</td>
									<td>{{item.ProcessingMethod}}</td>
									<td>{{item.OnStandard}}</td>
									<td style="text-align: center;">
										<uni-data-checkbox v-model="item.FResult" :localdata="sexs" />
									</td>
								</tr>
							</table>

						</view>
					</view>

					<view class="bottomBtnsOne">
						<button block @click="submit">
							保 &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 存
						</button>
					</view>

					<u-action-sheet :actions="PeriodTypeList" :show="PeriodTypeShow" @select="PeriodTypeClick"
						@close="PeriodTypeShow=false"></u-action-sheet>
					<u-toast ref="uToast"></u-toast>
					<u-datetime-picker :show="Timeshow" v-model="value1" mode="date" @cancel='Timeshow=false'
						@close="Timeshow=false" @confirm='pickerConfirm'></u-datetime-picker>

				</view>
			</template>
		</mc-framework>
	</view>
</template>

<script>
	import {
		getTimeStamp,
		getFormatDateTime,
		getBefore7DateTime
	} from '@/mes/common/base.js';
	export default {
		data() {
			return {

				baseFormData: {
					EquipmentName: '',
					EquipmentID: '',
					PeriodTypeName: '日',
					PeriodType: 'D',
					CheckupMan: '',
					BillDate: ''

				},
				PeriodTypeShow: false,
				PeriodTypeList: [{
						name: '月',
						value: 'M'
					},
					{
						name: '周',
						value: 'W'
					},

					{
						name: '日',
						value: 'D'
					},
				],
				sexs: [{
					text: '合格',
					value: 0
				}, {
					text: '不合格',
					value: 1
				}],
				tableData: [],
				routeIndex: 0,
				Timeshow: false,
				value1: Number(new Date()),
			};
		},
		onLoad(item) {
			var that = this

			if (item.CheckupEquipmentID) {
				that.queryList(item.CheckupEquipmentID)
			} else {
				this.baseFormData.CheckupMan = this.baseFormData.CheckupMan ? this.baseFormData.CheckupMan : this
					.$store.state.UserInfo.UserName
				this.baseFormData.CheckupID = this.baseFormData.CheckupID ? this.baseFormData.CheckupID : this.$store
					.state.UserInfo.EmplID
				this.baseFormData.EquipmentName = this.baseFormData.EquipmentName ? this.baseFormData.EquipmentName : this
					.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentName
				this.baseFormData.EquipmentID = this.baseFormData.EquipmentID ? this.baseFormData.EquipmentID : this
					.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentID
				this.baseFormData.BillDate = this.baseFormData.BillDate ? this.baseFormData.BillDate :
					getFormatDateTime(new Date())
				that.addPeriodTypeClick()
			}

		},
		onBackPress(options) { // 监听页面返回
			if (options.from == 'navigateBack') { // 通过点击路由返回
				return false;
			} else if (options.from == 'backbutton') { // 通过外置返回
				return true;
			}
		},
		methods: {
			// 获取时间
			pickerConfirm(e) {
				var time = getFormatDateTime(e.value)
				this.baseFormData.BillDate = time
				this.Timeshow = false
				this.$forceUpdate()
			},
			addPeriodTypeClick() {
				var that = this
				if (that.baseFormData.PeriodType == 'D') {
					that.baseFormData.PeriodTypeName = '日'
				} else if (that.baseFormData.PeriodType == 'M') {
					that.baseFormData.PeriodTypeName = '月'
				} else {
					that.baseFormData.PeriodTypeName = '周'
				}
				var obj = {
					value: that.baseFormData.PeriodType,
					name: that.baseFormData.PeriodTypeName
				}
				that.PeriodTypeClick(obj)
			},
			PeriodTypeClick(e) {
				var that = this
				that.baseFormData.PeriodType = e.value
				that.baseFormData.PeriodTypeName = e.name
				that.PeriodTypeShow = false
				that.Request.request('getCheckupItem', {
					PeriodType: e.value,
					FType: 'SBDJ'
				}, (ret) => {
					ret.data.ds.map(item => item.FResult = item.FResult ? item.FResult : 0)
					ret.data.ds.map(item => item.CheckupEquipmentDeID = item.CheckupEquipmentDeID ? item
						.CheckupEquipmentDeID : 0)
					that.tableData = ret.data.ds
					that.$forceUpdate()
				})
			},

			queryList(id) {
				var that = this
				try {
					that.Request.request('getCheckuEquipmentDetail', {
						CheckupEquipmentID: id
					}, (ret) => {
						console.log(ret.data);
						ret.data.ds[0].BillDate = ret.data.ds[0].BillDate ? ret.data.ds[0].BillDate.split('T')[0] :
							''
						that.baseFormData = ret.data.ds[0]

						that.tableData = ret.data.ds1
						that.$forceUpdate()
					})
				} catch (e) {
					console.log(e)
					this.$utils.hideLoading();
					this.$utils.msg('请求失败请重试')
				}
			},
			submit() {
				var that = this
				var show = true
				var list = JSON.parse(JSON.stringify(that.tableData))
				if (show) {
					list.forEach(item => {
						if (item.ProcessingMethod == '' || item.ProcessingMethod == null) {
							item.ProcessingMethod = '@'
						}
						if (item.OnStandard == '' || item.OnStandard == null) {
							item.OnStandard = '@'
						}
					})
					var obj = {
						CheckupMan: that.baseFormData.CheckupMan,
						EquipmentID: that.baseFormData.EquipmentID,
						CheckupEquipmentID: that.baseFormData.CheckupEquipmentID ? that.baseFormData
							.CheckupEquipmentID : 0,
						PeriodType: that.baseFormData.PeriodType,
						OnStandard: list.map(item => item.OnStandard).join(','),
						ProcessingMethod: list.map(item => item.ProcessingMethod).join(','),
						UpID: list.map(item => item.UpID).join(','),
						CheckupItemID: list.map(item => item.CheckupItemID).join(','),
						FResult: list.map(item => item.FResult).join(','),
						BillDate: that.baseFormData.BillDate,
						CheckupEquipmentDeID: list.map(item => item.CheckupEquipmentDeID).join(',')
					}
					console.log(obj);
					that.Request.request('saveCheckupEquipment', obj, function(ret) {
						console.log(ret);
						if (ret.status == 200) {
							that.$refs.uToast.show({
								title: '保存',
								type: 'success',
								message: '保存成功'
							})
							that.queryList(ret.data.ds[0].CheckupEquipmentID)
						}
					})

				}

			},
			changeLevel(data, row) {

				let routes = getCurrentPages(); //当前页面栈

				let backIndex = routes.length - row.length; // 计算返回页面栈数
				if (backIndex == 1) {
					let prevPage = routes[routes.length - 2]
					prevPage.$vm.getList()
				}
				uni.navigateBack({
					delta: backIndex
				})
			},
		}
	}
</script>

<style lang="scss">
	@import "../css/index.css";

	.s {
		border-bottom: 2px solid #51d0d0;
	}
</style>